﻿using Dispatcher.Core.Mail;
using Dispatcher.Core.Mail.Models;
using Dispatcher.Data.Entities.MySql;
using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Utility;
using System.Net.Mail;

namespace Dispatcher.Data
{
    [AutoResolve]
    public class MailDataProvider : EntityDataProvider<DispatcherContext>, IMailProvider
    {
        public void SentLog(string messageId, MailSendRequest request, MailMessage message, bool enableAsync, bool cancelled)
        {
            MailSent mailSent = new()
            {
                MessageId = messageId,
                AccountName = request == null ? string.Empty : request.AccountName,
                TemplateName = request == null ? string.Empty : request.TemplateName,
                SendData = request == null ? string.Empty : request.Data.ToJsonString(),
                IsAsync = enableAsync,
                Sender = message == null ? string.Empty : message.From.ToString(),
                Receiver = message == null ? string.Empty : message.To.ToString(),
                Title = message == null ? string.Empty : message.Subject,
                Body = message == null ? string.Empty : message.Body,
                CcList = message == null ? string.Empty : message.CC.ToString(),
                BccList = message == null ? string.Empty : message.Bcc.ToString(),
                SendTime = DateTime.Now
            };
            this.Insert(mailSent);
        }

        public void FailLog(string messageId, MailSendRequest request, MailMessage message, bool enableAsync, Exception exception)
        {
            //var dataOperation = this.GetDataCommand("insertMailSentFail");
            //dataOperation.ExecuteOperation();
            MailSentFail mailSentFail = new()
            {
                MessageId = messageId,
                AccountName = request == null ? string.Empty : request.AccountName,
                TemplateName = request == null ? string.Empty : request.TemplateName,
                SendData = request == null ? string.Empty : request.Data.ToJsonString(),
                IsAsync = enableAsync,
                Sender = message == null ? string.Empty : message.From.ToString(),
                Receiver = message == null ? string.Empty : message.To.ToString(),
                Title = message == null ? string.Empty : message.Subject,
                Body = message == null ? string.Empty : message.Body,
                CcList = message == null ? string.Empty : message.CC.ToString(),
                BccList = message == null ? string.Empty : message.Bcc.ToString(),
                SendTime = DateTime.Now,
                Exception = exception == null ? string.Empty : exception.ToString()
            };
        }

    }
}
